package com.cn.onetrip.activity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SDCardUtils;
import com.cn.onetrip.view.VideoPlayer;
//import com.cn.onetrip.view.VideoPlayer;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import android.widget.ProgressBar;
import android.widget.Toast;
//import io.vov.vitamio.MediaPlayer;
//import io.vov.vitamio.MediaPlayer.OnErrorListener;
//import io.vov.vitamio.MediaPlayer.OnCompletionListener;
//import io.vov.vitamio.MediaPlayer.OnInfoListener;
//import io.vov.vitamio.MediaPlayer.OnPreparedListener;
//import io.vov.vitamio.widget.MediaController;
//import io.vov.vitamio.widget.VideoView;

public class ScenicVideoPlayActivity extends Activity {

	public static final String TAG = "VideoPlayer";
//	private VideoView mVideoView;
	private VideoPlayer contentVideoPlayer;
	private int mPositionWhenPaused = -1;
	private int activityFlag = 0;
	private ProgressBar pb;
	private String videoUrl = "";
	private String HeadMovieString = "";
	private boolean isHeadMovie = true;
	private boolean isHomePress = false;
	
	private HomeKeyEventBroadCastReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_play_video);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		HeadMovieString = SDCardUtils
				.getFlashPath(ScenicVideoPlayActivity.this)
				+ "OneTripMap/ocr/headMovie/MovieHeader.mp4";

		contentVideoPlayer = (VideoPlayer) findViewById(R.id.video_player_content);
		pb = (ProgressBar) findViewById(R.id.pb_loading);
		
		receiver = new HomeKeyEventBroadCastReceiver();
		registerReceiver(receiver, new IntentFilter(
				Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		videoUrl = bundle.getString("videoUrl");
		
		contentVideoPlayer.startPlaying(HeadMovieString, handler);
//		contentVideoPlayer.startPlaying(videoUrl, handler);
	}
	
	Handler handler = new Handler(){
		public void handleMessage(Message msg) {

			
			switch (msg.what) {
			case VideoPlayer.START_LOADING:
				pb.setVisibility(View.VISIBLE);
				break;
				
			case VideoPlayer.LOADING_END:
				pb.setVisibility(View.GONE);
				break;
				
			case VideoPlayer.PLAYING_END: {
				if (!isHomePress) {
					if (isHeadMovie) {
						isHeadMovie = false;
						pb.setVisibility(View.VISIBLE);
						contentVideoPlayer.startPlaying(videoUrl, handler);
					} else {
						activitySwap();
					}
				}
			}
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void onPause() {
		Log.e("activity", "onPause");
		// Stop video when the activity is pause.
		mPositionWhenPaused = contentVideoPlayer.getCurPos();
		contentVideoPlayer.pause();

		super.onPause();
	}

	@Override
	public void onResume() {
		Log.e("activity", "onResume");
		// Resume video player
		if (mPositionWhenPaused >= 0) {
			contentVideoPlayer.seekTo(mPositionWhenPaused);
			
			mPositionWhenPaused = -1;
			contentVideoPlayer.play();
		}
		isHomePress = false;
		super.onResume();
	}
	
	@Override
	public void onDestroy() {
		contentVideoPlayer.stop();
		unregisterReceiver(receiver);
		
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			activitySwap();
		} 
		return super.onKeyDown(keyCode, event);
	}

	private void activitySwap() {
		finish();
	}
	
	class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
		static final String SYSTEM_REASON = "reason";
		static final String SYSTEM_HOME_KEY = "homekey";// home key
		static final String SYSTEM_RECENT_APPS = "recentapps";// long home key
		static final String SYSTEM_LOCK = "lock";

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
				String reason = intent.getStringExtra(SYSTEM_REASON);
				if (reason != null) {
					Log.e("reason-", reason);
					if (reason.equals(SYSTEM_HOME_KEY) || reason.equals(SYSTEM_LOCK)) {
						// home key
						isHomePress = true;
						Log.e("onKeyDown", "Home press");
					} else if (reason.equals(SYSTEM_RECENT_APPS)) {
						// long home key
					}
				}
			}
		}
	}
}
